package learnjava.concur.chapter5;

public class P013 {

    /**
     * 测试volatile读写屏障
     * @param args
     */

//    volatile static boolean flag = false;
    static int num = 0;
    static boolean flag = false;
    static int res = -1;

    public static void main(String[] args) throws Exception{


        Thread t1 = new Thread(()->{
            if(flag){
                res = num + num;
            }else {
                res = 1;
            }
        });

        Thread t2 = new Thread(()->{
            num = 2;
            flag = true;
        });


        t2.start();t1.start();
        t1.join();
        t2.join();
        System.out.println(res);
    }
}
